


public class Cyclic2DArrayPrint {
	public static int no_of_row = 4;
	public static int no_of_column = 6;
	public static int a[][]= {{1,2,3,4,5,6},
							  {1,2,3,4,5,6},
							  {1,2,3,4,5,6},
							  {1,2,3,4,5,6},
			//				  {1,2,3,4,5,6},
			//				  {1,2,3,4,5,6}
							  };
	
	public static void printArray(){
		for(int i= 0 ;i< 6;i++){for(int j=0;j<6;j++){System.out.print(a[i][j]+"\t");a[i][j]=-1;}System.out.println();}
	}
	
	public static void printCyclic(){
int x=0;
int y= no_of_column -1;
int x1= no_of_row- 1;
		try {
			while(true){
		printLeft2Right(x,x,x,y);
		printTop2Bottom(x+1,y,x1,y);
		printRight2Left(x1,y-1,x1,x);
		printBottom2Top(x1-1,x,x+1,x);
		x++;
		y--;
		x1--;
			}
		} catch (NullPointerException e){
			System.out.println("Traversal is done NE");
		} catch (Exception e){
			System.out.println("Traversal is done  E");
		}
		
		
	}
	private static void printBottom2Top (int y, int x, int i1, int x2) {
		// TODO Auto-generated method stub
		for(int i =y;i>=i1;i--){ if(a[i][x] ==-1) {throw new NullPointerException();}System.out.print(a[i][x]+" ");a[i][x]=-1;};
	}

	private static void printRight2Left(int y, int y2, int y3, int x) {
		// TODO Auto-generated method stub
		for(int i =y2;i>=x;i--){if(a[y][i] ==-1) {throw new NullPointerException();}System.out.print(a[y][i]+" ");a[y][i]=-1;}
		
	}

	private static void printTop2Bottom(int x, int y, int y2, int y3) {
		// TODO Auto-generated method stub
//		System.out.println("calling top to bottom");
		for(int i =x;i<=y2;i++){if(a[i][y] ==-1) {throw new NullPointerException();}System.out.print(a[i][y]+" ");a[i][y]=-1;}
	}

	private static void printLeft2Right(int x, int x2, int x3, int y) {
		for(int i =x2;i<=y;i++){if(a[x][i] ==-1) {throw new NullPointerException();}System.out.print(a[x][i]+" ");a[x][i]=1;}
		
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	//	printArray();
		printCyclic();
	}

}
